package net.jonbuck.tassoo.ui.view.comparator;

import net.jonbuck.tassoo.model.Attachment;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;

/**
 * 
 * 
 * @since 1.0.0
 */
public class AttachmentsTableComparator extends ViewerComparator {

	/** ... */
	private int direction = SWT.None;

	/** ... */
	private int tableColumn;

	/**
	 * 
	 */
	public int compare(Viewer viewer, Object e1, Object e2) {
		int rc = 0;
		Attachment attachment1 = (Attachment) e1;
		Attachment attachment2 = (Attachment) e2;
		if (direction != SWT.NONE) {
			switch (tableColumn) {
			case 0:
				rc = attachment1.getType().compareTo(attachment2.getType());
				break;
			case 1:
				rc = attachment1.getLabel().compareTo(attachment2.getLabel());
				break;
			case 3:
				rc = attachment1.getLocation().compareTo(attachment2.getLocation());
				break;
			}
		}

		// If descending order, flip the direction
		if (direction == SWT.DOWN) {
			rc = -rc;
		}

		return rc;
	}

	/**
	 * 
	 * @param trclmn
	 */
	public void setColumn(int tblclmn) {
		this.tableColumn = tblclmn;
	}

	/**
	 * 
	 * @param dir
	 */
	public void setDirection(int dir) {
		this.direction = dir;
	}
}
